package com.sgcc.pda.mdrh.h5.jsbridge;

import android.util.Log;

import com.sgcc.pda.CommonResultsFactory;
import com.sgcc.pda.common.aidl.CommonParams;
import com.sgcc.pda.common.aidl.CommonResults;
import com.sgcc.pda.mdrh.h5.library.BridgeHandler;
import com.sgcc.pda.mdrh.h5.library.CallBackFunction;

/**
 * @author superFan
 * date:: 2019/7/16/016 16:12
 * company:: beijing_hezhongweiqi
 * description:: H5桥的处理handler 内含有 对参数的判断逻辑 是BridgeHandler的实现
 */
public abstract class MixBridgeHandlerImpl implements BridgeHandler {
    private static final String TAG = "MixBridgeHandlerImpl";

    @Override
    public void handler(String data, CallBackFunction function) {
        Log.d(TAG, Thread.currentThread().getName());
        Log.d(TAG, "从H5传过来的数据: " + data);
        CommonParams commonParams;
        try {
            commonParams = new CommonParams().fromJSON(data);
        } catch (Exception e) {
            CommonResults error = CommonResultsFactory.parametersExceptionResults();
            error.setMsg("参数格式错误，正确格式为：" + new CommonParams().toJSON()
                    + "\n传过来的数据为=" + data);
            function.onCallBack(error.toJSON());
            return;
        }
        mixHandler(data, commonParams, function);
    }

    public abstract void mixHandler(String originalData, CommonParams commonParams, CallBackFunction function);
}
